<template>
  <div class="china-map">
    <!-- ECharts 容器 -->
    <div id="mapChart" style="width: 100%; height: 900px"></div>
  </div>
</template>

<script setup>
  import { onMounted } from 'vue';
  import * as echarts from 'echarts';
  import chinaMapData from './china.json';
  const props = defineProps({
    policyData: Array,
  });
  onMounted(() => {
    const myChart = echarts.init(document.getElementById('mapChart'));

    // 注册地图
    echarts.registerMap('china', chinaMapData);

    const option = {
      tooltip: {
        trigger: 'item',
        formatter: (params) => `${params.name}<br>政策数：${params.value || 0}`,
      },
      // 3D效果通过多层geo组件叠加阴影实现
      geo: [
        {
          // 主地图层
          map: 'china',
          zoom: 1,
          layoutCenter: ['50%', '50%'],
          layoutSize: '100%',
          show: true,
          roam: false,
          label: {
            show: true, // 显示省份名称
            color: '#fff',
            fontSize: 10,
            avoidLabelOverlap: true, // 避免标签重叠
          },
          itemStyle: {
            areaColor: {
              type: 'linear',
              x: 1200,
              y: 0,
              x2: 0,
              y2: 0,
              colorStops: [
                { offset: 0, color: 'rgba(3,27,78,0.75)' },
                { offset: 1, color: 'rgba(58,149,253,0.75)' },
              ],
              global: true,
            },
            borderColor: '#c0f3fb',
            borderWidth: 0.8,
          },
          emphasis: {
            itemStyle: {
              areaColor: 'rgba(0,254,233,0.6)',
            },
            label: {
              show: true,
              color: '#fff',
              fontSize: 12,
            },
          },
        },
        // 底层阴影层（多层叠加增强3D效果）
        {
          type: 'map',
          map: 'china',
          zlevel: -1,
          zoom: 1,
          layoutCenter: ['50%', '51%'],
          layoutSize: '100%',
          roam: false,
          silent: true,
          itemStyle: {
            borderWidth: 1,
            borderColor: '#498BDE',
            shadowColor: '#498BDE',
            shadowOffsetY: 5,
            shadowBlur: 15,
            areaColor: '#90A7D4',
          },
        },
      ],
      // 数据系列绑定到geo组件
      series: [
        {
          name: '政策数',
          type: 'map',
          map: 'china',
          data: props.policyData,
          geoIndex: 0, // 关联到第一个geo组件
          label: {
            show: true,
            fontSize: 10,
            color: '#fff',
            fontWeight: 'normal',
            position: 'inside',
            formatter: '{b}',
            avoidLabelOverlap: true,
          },
          emphasis: {
            label: {
              show: true,
              fontSize: 12,
              fontWeight: 'bold',
            },
          },
        },
      ],
    };

    myChart.setOption(option);
  });
</script>

<style scoped>
  .china-map {
    position: relative;
    width: 100%;
    height: 900px;
  }
</style>
